<?php

/**
 * SocialEngine
 *
 * @category   Application_Extensions
 * @package    Sitepage
 * @copyright  Copyright 2010-2011 BigStep Technologies Pvt. Ltd.
 * @license    http://www.socialengineaddons.com/license/
 * @version    $Id: install.php 2011-05-05 9:40:21Z SocialEngineAddOns $
 * @author     SocialEngineAddOns
 */
class Sitepage_Installer extends Engine_Package_Installer_Module {
	
	function onPreInstall() {
		$PRODUCT_TYPE = 'sitepage';
		$PLUGIN_TITLE = 'Sitepage';
		$PLUGIN_VERSION = '4.1.8';
		$PLUGIN_CATEGORY = 'plugin';
		$PRODUCT_DESCRIPTION = 'Sitepage Plugin';
		$PRODUCT_TITLE = 'Directory / Pages';
		$_PRODUCT_FINAL_FILE = 0;
		$SocialEngineAddOns_version = '4.1.8';
		$file_path = APPLICATION_PATH . "/application/modules/$PLUGIN_TITLE/controllers/license/ilicense.php";
		$is_file = file_exists ( $file_path );
		if (empty ( $is_file )) {
			include APPLICATION_PATH . "/application/modules/$PLUGIN_TITLE/controllers/license/license4.php";
		} else {
			include APPLICATION_PATH . "/application/modules/$PLUGIN_TITLE/controllers/license/license3.php";
			include $file_path;
		}
		parent::onPreInstall ();
	}
	
	function onInstall() {
		
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepage' )->where ( 'version <= ?', '4.1.5p1' );
		$is_enabled = $select->query ()->fetchObject ();
		if (! empty ( $is_enabled )) {
			$db->query ( "DROP TABLE IF EXISTS `engine4_sitepage_admincontent`;" );
			$db->query ( "CREATE TABLE IF NOT EXISTS `engine4_sitepage_admincontent` (
								  `admincontent_id` int(11) unsigned NOT NULL AUTO_INCREMENT,
								  `page_id` int(11) unsigned NOT NULL,
								  `type` varchar(32) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL DEFAULT 'widget',
								  `name` varchar(64) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL,
								  `parent_content_id` int(11) unsigned DEFAULT NULL,
								  `order` int(11) NOT NULL DEFAULT '1',
								  `params` text COLLATE utf8_unicode_ci,
								  `attribs` text COLLATE utf8_unicode_ci,
								  PRIMARY KEY (`admincontent_id`),
								  KEY `page_id` (`page_id`,`order`)
								) ENGINE=InnoDB  DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=1;" );
			
			$db->query ( "DROP TABLE IF EXISTS `engine4_sitepage_hideprofilewidgets`;" );
			$db->query ( "CREATE TABLE IF NOT EXISTS `engine4_sitepage_hideprofilewidgets` (
								  `hideprofilewidgets_id` int(11) NOT NULL AUTO_INCREMENT,
								  `widgetname` varchar(64) NOT NULL,
								  PRIMARY KEY (`hideprofilewidgets_id`)
								) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1;" );
			
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_pages', array ('page_id' ) )->where ( 'name = ?', 'sitepage_index_view' );
			$corepageObject = $select->query ()->fetchAll ();
			
			if (! empty ( $corepageObject )) {
				$page_id = $corepageObject [0] ['page_id'];
			}
			if (! empty ( $page_id )) {
				$select = new Zend_Db_Select ( $db );
				$db->query ( "DELETE FROM engine4_sitepage_hideprofilewidgets" );
				
				if (! empty ( $page_id )) {
					$select = new Zend_Db_Select ( $db );
					$db->query ( "DELETE FROM engine4_sitepage_admincontent WHERE page_id = $page_id" );
				}
				$select = new Zend_Db_Select ( $db );
				$select->from ( 'engine4_core_settings', array ('value' ) )->where ( 'name = ?', 'sitepage.layout.setting' );
				$layoutsetting = $select->query ()->fetchAll ();
				$select = new Zend_Db_Select ( $db );
				$select->from ( 'engine4_core_settings', array ('value' ) )->where ( 'name = ?', 'sitepage.showmore' );
				$showmore = $select->query ()->fetchAll ();
				
				if (! empty ( $showmore )) {
					$showmaxtab = $showmore [0] ['value'];
					$maxtab = "{\"max\":\"$showmaxtab\"}";
				} else {
					$maxtab = "{\"max\":\"8\"}";
				}
				
				if ($layoutsetting [0] ['value'] == 1) {
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`) VALUES
					($page_id, 'container', 'main', '2')" );
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_sitepage_admincontent', array ('admincontent_id' ) )->where ( 'name = ?', 'main' )->where ( 'type = ?', 'container' );
					$containerObject = $select->query ()->fetchAll ();
					$container_id = $containerObject [0] ['admincontent_id'];
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
		($page_id, 'container', 'middle', '6', $container_id)" );
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_sitepage_admincontent', array ('admincontent_id' ) )->where ( 'name = ?', 'middle' )->where ( 'type = ?', 'container' );
					$containerObject = $select->query ()->fetchAll ();
					$middle_id = $containerObject [0] ['admincontent_id'];
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
		($page_id, 'container', 'left', '4', $container_id)" );
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_sitepage_admincontent', array ('admincontent_id' ) )->where ( 'name = ?', 'left' )->where ( 'type = ?', 'container' );
					$containerObject = $select->query ()->fetchAll ();
					$left_id = $containerObject [0] ['admincontent_id'];
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
			($page_id, 'widget', 'core.container-tabs', '7', $middle_id, '$maxtab')" );
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_sitepage_admincontent', array ('admincontent_id' ) )->where ( 'name = ?', 'core.container-tabs' )->where ( 'type = ?', 'widget' );
					$containerObject = $select->query ()->fetchAll ();
					$middle_tab = $containerObject [0] ['admincontent_id'];
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
		($page_id, 'widget', 'sitepage.thumbphoto-sitepage', '1', $middle_id,'{\"title\":\"\"}')" );
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.title-sitepage', '2', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.page-like-button', '3', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'facebookse' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'Facebookse.facebookse-sitepageprofilelike', '4', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				  ($page_id, 'widget', 'sitepage.photorecent-sitepage', '5', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				  ($page_id, 'widget', 'sitepage.albums-sitepage', '23', $left_id,'{\"title\":\"Albums\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagemusic' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				  ($page_id, 'widget', 'sitepagemusic.profile-player', '24', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					}
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
			($page_id, 'widget', 'sitepage.favourite-page', '25', $left_id,'{\"title\":\"Linked Pages\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.mainphoto-sitepage', '10', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.options-sitepage', '11', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.write-page', '12', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.information-sitepage', '13', $left_id,'{\"title\":\"Information\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.page-like', '14', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepagereview.ratings-sitepagereviews', '15', $left_id,'{\"title\":\"Ratings\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagebadge' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepagebadge.badge-sitepagebadge', '16', $left_id,'{\"title\":\"Badge\",\"titleCount\":\"true\"}')" );
					}
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.suggestedpage-sitepage', '17', $left_id,'{\"title\":\"You May Also Like\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.socialshare-sitepage', '18', $left_id,'{\"title\":\"Social Share\",\"titleCount\":\"true\"}')" );
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.foursquare-sitepage', '19', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.insights-sitepage', '21', $left_id,'{\"title\":\"Insights\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.featuredowner-sitepage', '22', $left_id,'{\"title\":\"Owners\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'activity.feed', '1', $middle_tab,'{\"title\":\"Updates\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.info-sitepage', '2', $middle_tab,'{\"title\":\"Info\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.overview-sitepage', '3', $middle_tab,'{\"title\":\"Overview\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
			($page_id, 'widget', 'sitepage.location-sitepage', '4', $middle_tab,'{\"title\":\"Map\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
			($page_id, 'widget', 'core.profile-links', '125', $middle_tab,'{\"title\":\"Links\",\"titleCount\":\"true\"}')" );
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.photos-sitepage', '110', $middle_tab,'{\"title\":\"Photos\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagevideo' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepagevideo.profile-sitepagevideos', '111', $middle_tab,'{\"title\":\"Videos\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagenote' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepagenote.profile-sitepagenotes', '112', $middle_tab,'{\"title\":\"Notes\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepagereview.profile-sitepagereviews', '113', $middle_tab,'{\"title\":\"Reviews\",\"titleCount\":\"true\"}')" );
					}
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageform' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepageform.sitepage-viewform', '114', $middle_tab,'{\"title\":\"Form\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagedocument' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepagedocument.profile-sitepagedocuments', '115', $middle_tab,'{\"title\":\"Documents\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageoffer' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepageoffer.profile-sitepageoffers', '116', $middle_tab,'{\"title\":\"Offers\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageevent' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepageevent.profile-sitepageevents', '117', $middle_tab,'{\"title\":\"Events\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagepoll' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepagepoll.profile-sitepagepolls', '118', $middle_tab,'{\"title\":\"Polls\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagediscussion' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
				($page_id, 'widget', 'sitepage.discussion-sitepage', '119', $middle_tab,'{\"title\":\"Discussions\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					
					$select->from ( 'engine4_sitepage_contentpages', array ('contentpage_id' ) )->where ( 'name =?', 'sitepage_index_view' );
					
					$contentpages_id = $select->query ()->fetchAll ();
					
					foreach ( $contentpages_id as $key => $value ) {
						$page_id = $value ['contentpage_id'];
						
						$db->query ( "DELETE FROM engine4_sitepage_content WHERE contentpage_id = $page_id" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`) VALUES
					($page_id, 'container', 'main', '2')" );
						
						$select = new Zend_Db_Select ( $db );
						
						$select->from ( 'engine4_sitepage_content', array ('content_id' ) )->where ( 'name = ?', 'main' )->where ( 'type = ?', 'container' )->where ( 'contentpage_id = ?', $page_id );
						
						$containerObject = $select->query ()->fetchAll ();
						$container_id = $containerObject [0] ['content_id'];
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
		($page_id, 'container', 'middle', '6', $container_id)" );
						$select = new Zend_Db_Select ( $db );
						
						$select->from ( 'engine4_sitepage_content', array ('content_id' ) )->where ( 'name = ?', 'middle' )->where ( 'type = ?', 'container' )->where ( 'contentpage_id = ?', $page_id );
						$containerObject = $select->query ()->fetchAll ();
						$middle_id = $containerObject [0] ['content_id'];
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
		($page_id, 'container', 'left', '4', $container_id)" );
						$select = new Zend_Db_Select ( $db );
						
						$select->from ( 'engine4_sitepage_content', array ('content_id' ) )->where ( 'name = ?', 'left' )->where ( 'type = ?', 'container' )->where ( 'contentpage_id = ?', $page_id );
						$containerObject = $select->query ()->fetchAll ();
						$left_id = $containerObject [0] ['content_id'];
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
	($page_id, 'widget', 'core.container-tabs', '7', $middle_id, '$maxtab')" );
						$select = new Zend_Db_Select ( $db );
						
						$select->from ( 'engine4_sitepage_content', array ('content_id' ) )->where ( 'name = ?', 'core.container-tabs' )->where ( 'type = ?', 'widget' )->where ( 'contentpage_id = ?', $page_id );
						;
						$containerObject = $select->query ()->fetchAll ();
						$middle_tab = $containerObject [0] ['content_id'];
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
	($page_id, 'widget', 'sitepage.thumbphoto-sitepage', '1', $middle_id,'{\"title\":\"\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
						($page_id, 'widget', 'sitepage.title-sitepage', '2', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
						($page_id, 'widget', 'sitepage.page-like-button', '3', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'facebookse' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'Facebookse.facebookse-sitepageprofilelike', '4', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.photorecent-sitepage', '5', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
							
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.albums-sitepage', '23', $left_id,'{\"title\":\"Albums\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagemusic' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							  ($page_id, 'widget', 'sitepagemusic.profile-player', '24', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						}
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
			($page_id, 'widget', 'sitepage.favourite-page', '25', $left_id,'{\"title\":\"Linked Pages\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.mainphoto-sitepage', '10', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.options-sitepage', '11', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.write-page', '12', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.information-sitepage', '13', $left_id,'{\"title\":\"Information\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.page-like', '14', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagereview.ratings-sitepagereviews', '15', $left_id,'{\"title\":\"Ratings\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagebadge' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagebadge.badge-sitepagebadge', '16', $left_id,'{\"title\":\"Badge\",\"titleCount\":\"true\"}')" );
						}
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.suggestedpage-sitepage', '17', $left_id,'{\"title\":\"You May Also Like\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.socialshare-sitepage', '18', $left_id,'{\"title\":\"Social Share\",\"titleCount\":\"true\"}')" );
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.foursquare-sitepage', '19', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.insights-sitepage', '21', $left_id,'{\"title\":\"Insights\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.featuredowner-sitepage', '22', $left_id,'{\"title\":\"Owners\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'activity.feed', '1', $middle_tab,'{\"title\":\"Updates\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.info-sitepage', '2', $middle_tab,'{\"title\":\"Info\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.overview-sitepage', '3', $middle_tab,'{\"title\":\"Overview\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.location-sitepage', '4', $middle_tab,'{\"title\":\"Map\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'core.profile-links', '125', $middle_tab,'{\"title\":\"Links\",\"titleCount\":\"true\"}')" );
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.photos-sitepage', '110', $middle_tab,'{\"title\":\"Photos\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagevideo' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagevideo.profile-sitepagevideos', '111', $middle_tab,'{\"title\":\"Videos\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagenote' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagenote.profile-sitepagenotes', '112', $middle_tab,'{\"title\":\"Notes\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagereview.profile-sitepagereviews', '113', $middle_tab,'{\"title\":\"Reviews\",\"titleCount\":\"true\"}')" );
						}
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageform' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageform.sitepage-viewform', '114', $middle_tab,'{\"title\":\"Form\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagedocument' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagedocument.profile-sitepagedocuments', '115', $middle_tab,'{\"title\":\"Documents\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageoffer' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageoffer.profile-sitepageoffers', '116', $middle_tab,'{\"title\":\"Offers\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageevent' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageevent.profile-sitepageevents', '117', $middle_tab,'{\"title\":\"Events\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagepoll' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagepoll.profile-sitepagepolls', '118', $middle_tab,'{\"title\":\"Polls\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagediscussion' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.discussion-sitepage', '119', $middle_tab,'{\"title\":\"Discussions\",\"titleCount\":\"false\"}')" );
						}
					}
				} else {
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`) VALUES
	($page_id, 'container', 'main', '2')" );
					
					$select = new Zend_Db_Select ( $db );
					
					$select->from ( 'engine4_sitepage_admincontent', array ('admincontent_id' ) )->where ( 'name = ?', 'main' )->where ( 'type = ?', 'container' );
					
					$containerObject = $select->query ()->fetchAll ();
					$container_id = $containerObject [0] ['admincontent_id'];
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
	($page_id, 'container', 'middle', '6', $container_id)" );
					$select = new Zend_Db_Select ( $db );
					
					$select->from ( 'engine4_sitepage_admincontent', array ('admincontent_id' ) )->where ( 'name = ?', 'middle' )->where ( 'type = ?', 'container' );
					$containerObject = $select->query ()->fetchAll ();
					$middle_id = $containerObject [0] ['admincontent_id'];
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
	($page_id, 'container', 'left', '4', $container_id)" );
					$select = new Zend_Db_Select ( $db );
					
					$select->from ( 'engine4_sitepage_admincontent', array ('admincontent_id' ) )->where ( 'name = ?', 'left' )->where ( 'type = ?', 'container' );
					$containerObject = $select->query ()->fetchAll ();
					$left_id = $containerObject [0] ['admincontent_id'];
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
						($page_id, 'widget', 'sitepage.title-sitepage', '2', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
						($page_id, 'widget', 'sitepage.page-like-button', '3', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'facebookse' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'Facebookse.facebookse-sitepageprofilelike', '4', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagemusic' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							  ($page_id, 'widget', 'sitepagemusic.profile-player', '24', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					}
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
			($page_id, 'widget', 'sitepage.favourite-page', '25', $left_id,'{\"title\":\"Linked Pages\",\"titleCount\":\"true\"}')" );
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.photorecent-sitepage', '5', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.albums-sitepage', '23', $left_id,'{\"title\":\"Albums\",\"titleCount\":\"true\"}')" );
					}
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.mainphoto-sitepage', '10', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.widgetlinks-sitepage', '11', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.options-sitepage', '12', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.write-page', '13', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.information-sitepage', '14', $left_id,'{\"title\":\"Information\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.page-like', '15', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagereview.ratings-sitepagereviews', '16', $left_id,'{\"title\":\"Ratings\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagebadge' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagebadge.badge-sitepagebadge', '17', $left_id,'{\"title\":\"Badge\",\"titleCount\":\"true\"}')" );
					}
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.socialshare-sitepage', '18', $left_id,'{\"title\":\"Social Share\",\"titleCount\":\"true\"}')" );
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.foursquare-sitepage', '19', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.insights-sitepage', '21', $left_id,'{\"title\":\"Insights\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.featuredowner-sitepage', '22', $left_id,'{\"title\":\"Owners\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'activity.feed', '6', $middle_id,'{\"title\":\"Updates\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.info-sitepage', '7', $middle_id,'{\"title\":\"Info\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.overview-sitepage', '8', $middle_id,'{\"title\":\"Overview\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.location-sitepage', '9', $middle_id,'{\"title\":\"Map\",\"titleCount\":\"true\"}')" );
					
					$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'core.profile-links', '125', $middle_id,'{\"title\":\"Links\",\"titleCount\":\"true\"}')" );
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.photos-sitepage', '110', $middle_id,'{\"title\":\"Photos\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagevideo' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagevideo.profile-sitepagevideos', '111', $middle_id,'{\"title\":\"Videos\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagenote' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagenote.profile-sitepagenotes', '112', $middle_id,'{\"title\":\"Notes\",\"titleCount\":\"true\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagereview.profile-sitepagereviews', '113', $middle_id,'{\"title\":\"Reviews\",\"titleCount\":\"true\"}')" );
					}
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageform' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageform.sitepage-viewform', '114', $middle_id,'{\"title\":\"Form\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagedocument' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagedocument.profile-sitepagedocuments', '115', $middle_id,'{\"title\":\"Documents\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageoffer' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageoffer.profile-sitepageoffers', '116', $middle_id,'{\"title\":\"Offers\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageevent' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageevent.profile-sitepageevents', '117', $middle_id,'{\"title\":\"Events\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagepoll' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagepoll.profile-sitepagepolls', '118', $middle_id,'{\"title\":\"Polls\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagediscussion' );
					$is_enabled = $select->query ()->fetchObject ();
					if (! empty ( $is_enabled )) {
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_admincontent` (`page_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.discussion-sitepage', '119', $middle_id,'{\"title\":\"Discussions\",\"titleCount\":\"false\"}')" );
					}
					
					$select = new Zend_Db_Select ( $db );
					
					$select->from ( 'engine4_sitepage_contentpages', array ('contentpage_id' ) )->where ( 'name =?', 'sitepage_index_view' );
					
					$contentpages_id = $select->query ()->fetchAll ();
					
					foreach ( $contentpages_id as $key => $value ) {
						$page_id = $value ['contentpage_id'];
						
						$db->query ( "DELETE FROM engine4_sitepage_content WHERE contentpage_id = $page_id" );
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`) VALUES
							($page_id, 'container', 'main', '2')" );
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_sitepage_content', array ('content_id' ) )->where ( 'name = ?', 'main' )->where ( 'type = ?', 'container' )->where ( 'contentpage_id = ?', $page_id );
						
						$containerObject = $select->query ()->fetchAll ();
						$container_id = $containerObject [0] ['content_id'];
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
	($page_id, 'container', 'middle', '6', $container_id)" );
						$select = new Zend_Db_Select ( $db );
						
						$select->from ( 'engine4_sitepage_content', array ('content_id' ) )->where ( 'name = ?', 'middle' )->where ( 'type = ?', 'container' )->where ( 'contentpage_id = ?', $page_id );
						$containerObject = $select->query ()->fetchAll ();
						$middle_id = $containerObject [0] ['content_id'];
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`,`parent_content_id`) VALUES
	($page_id, 'container', 'left', '4', $container_id)" );
						$select = new Zend_Db_Select ( $db );
						
						$select->from ( 'engine4_sitepage_content', array ('content_id' ) )->where ( 'name = ?', 'left' )->where ( 'type = ?', 'container' )->where ( 'contentpage_id = ?', $page_id );
						$containerObject = $select->query ()->fetchAll ();
						$left_id = $containerObject [0] ['content_id'];
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
						($page_id, 'widget', 'sitepage.title-sitepage', '2', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
						($page_id, 'widget', 'sitepage.favourite-page', '25', $left_id,'{\"title\":\"Linked Pages\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
						($page_id, 'widget', 'sitepage.page-like-button', '3', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'facebookse' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'Facebookse.facebookse-sitepageprofilelike', '4', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.photorecent-sitepage', '5', $middle_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
							
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.albums-sitepage', '23', $left_id,'{\"title\":\"Albums\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagemusic' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							  ($page_id, 'widget', 'sitepagemusic.profile-player', '24', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						}
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.mainphoto-sitepage', '10', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.widgetlinks-sitepage', '11', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.options-sitepage', '12', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.write-page', '13', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.information-sitepage', '14', $left_id,'{\"title\":\"Information\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.page-like', '15', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagereview.ratings-sitepagereviews', '16', $left_id,'{\"title\":\"Ratings\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagebadge' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagebadge.badge-sitepagebadge', '17', $left_id,'{\"title\":\"Badge\",\"titleCount\":\"true\"}')" );
						}
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.socialshare-sitepage', '18', $left_id,'{\"title\":\"Social Share\",\"titleCount\":\"true\"}')" );
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.foursquare-sitepage', '19', $left_id,'{\"title\":\"\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.insights-sitepage', '21', $left_id,'{\"title\":\"Insights\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.featuredowner-sitepage', '22', $left_id,'{\"title\":\"Owners\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'activity.feed', '6', $middle_id,'{\"title\":\"Updates\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.info-sitepage', '7', $middle_id,'{\"title\":\"Info\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.overview-sitepage', '8', $middle_id,'{\"title\":\"Overview\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.location-sitepage', '9', $middle_id,'{\"title\":\"Map\",\"titleCount\":\"true\"}')" );
						
						$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'core.profile-links', '125', $middle_id,'{\"title\":\"Links\",\"titleCount\":\"true\"}')" );
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagealbum' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.photos-sitepage', '110', $middle_id,'{\"title\":\"Photos\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagevideo' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagevideo.profile-sitepagevideos', '111', $middle_id,'{\"title\":\"Videos\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagenote' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagenote.profile-sitepagenotes', '112', $middle_id,'{\"title\":\"Notes\",\"titleCount\":\"true\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagereview' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagereview.profile-sitepagereviews', '113', $middle_id,'{\"title\":\"Reviews\",\"titleCount\":\"true\"}')" );
						}
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageform' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageform.sitepage-viewform', '114', $middle_id,'{\"title\":\"Form\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagedocument' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagedocument.profile-sitepagedocuments', '115', $middle_id,'{\"title\":\"Documents\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageoffer' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageoffer.profile-sitepageoffers', '116', $middle_id,'{\"title\":\"Offers\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepageevent' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepageevent.profile-sitepageevents', '117', $middle_id,'{\"title\":\"Events\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagepoll' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepagepoll.profile-sitepagepolls', '118', $middle_id,'{\"title\":\"Polls\",\"titleCount\":\"false\"}')" );
						}
						
						$select = new Zend_Db_Select ( $db );
						$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepagediscussion' );
						$is_enabled = $select->query ()->fetchObject ();
						if (! empty ( $is_enabled )) {
							$db->query ( "INSERT IGNORE INTO `engine4_sitepage_content` (`contentpage_id`, `type`, `name`, `order`, `parent_content_id`, `params`) VALUES
							($page_id, 'widget', 'sitepage.discussion-sitepage', '119', $middle_id,'{\"title\":\"Discussions\",\"titleCount\":\"false\"}')" );
						}
					}
				}
			}
		}
		
		//CODE FOR INSERTING THE DATE COLUMN IN LIKES TABLE
		$db = $this->getDb ();
		
		// ============ Querys Start for "Suggestion Plugin". ================
		// Insert notification type for: Page Plugin
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'suggestion' );
		$is_enabled = $select->query ()->fetchObject ();
		if (! empty ( $is_enabled )) {
			// Notification  Type: Insert in data base.
			$db->query ( 'INSERT IGNORE INTO `engine4_activity_notificationtypes` (`type`, `module`, `body`, `is_request`, `handler`) VALUES
("page_suggestion", "suggestion", \'{item:$subject} has suggested to you a {item:$object:page}.\', 1, "suggestion.widget.get-notify")' );
			
			// Insert In Mailteplates :
			$db->query ( 'INSERT IGNORE INTO  `engine4_core_mailtemplates` ( `type` , `module` , `vars` ) VALUES ("notify_page_suggestion",  "suggestion",  "[suggestion_sender], [suggestion_entity], [email], [link]")' );
			
			// Insert into "core_settings" table.
			$db->query ( 'INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES ("sugg.sitepage.wid", 5), ("sitepage.sugg.link", 1), ("after.sitepage.create", 1)' );
			
			// Sitepage suggestion enabled or not : Value insert in data base.
			$table_exist = $db->query ( 'SHOW TABLES LIKE \'engine4_suggestion_notifications\'' )->fetch ();
			if (! empty ( $table_exist )) {
				$db->query ( 'INSERT IGNORE INTO `engine4_suggestion_notifications` (`type`, `status`) VALUES ("sitepage", 1)' );
			}
		}
		
		// ============ Querys End for "Suggestion Plugin". ================
		

		$type_array = $db->query ( "SHOW COLUMNS FROM engine4_core_likes LIKE 'creation_date'" )->fetch ();
		if (empty ( $type_array )) {
			$run_query = $db->query ( "ALTER TABLE `engine4_core_likes` ADD `creation_date` DATETIME NOT NULL" );
		}
		//CODE FOR INCREASE THE SIZE OF engine4_authorization_permissions's FIELD type
		$type_array = $db->query ( "SHOW COLUMNS FROM engine4_authorization_permissions LIKE 'type'" )->fetch ();
		if (! empty ( $type_array )) {
			$varchar = $type_array ['Type'];
			$length_varchar = explode ( "(", $varchar );
			$length = explode ( ")", $length_varchar [1] );
			$length_type = $length [0];
			if ($length_type < 32) {
				$run_query = $db->query ( "ALTER TABLE `engine4_authorization_permissions` CHANGE `type` `type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
			}
		}
		
		//CODE FOR INCREASE THE SIZE OF engine4_authorization_allow's FIELD type
		$type_array = $db->query ( "SHOW COLUMNS FROM engine4_authorization_allow LIKE 'resource_type'" )->fetch ();
		if (! empty ( $type_array )) {
			$varchar = $type_array ['Type'];
			$length_varchar = explode ( "(", $varchar );
			$length = explode ( ")", $length_varchar [1] );
			$length_type = $length [0];
			if ($length_type < 32) {
				$run_query = $db->query ( "ALTER TABLE `engine4_authorization_allow` CHANGE `resource_type` `resource_type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
			}
		}
		
		//CODE FOR INCREASE THE SIZE OF engine4_activity_attachments's FIELD type
		$type_array = $db->query ( "SHOW COLUMNS FROM engine4_activity_attachments LIKE 'type'" )->fetch ();
		if (! empty ( $type_array )) {
			$varchar = $type_array ['Type'];
			$length_varchar = explode ( "(", $varchar );
			$length = explode ( ")", $length_varchar [1] );
			$length_type = $length [0];
			if ($length_type < 32) {
				$run_query = $db->query ( "ALTER TABLE `engine4_activity_attachments` CHANGE `type` `type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
			}
		}
		
		//CODE FOR INCREASE THE SIZE OF engine4_activity_notifications's FIELD type
		$type_array = $db->query ( "SHOW COLUMNS FROM engine4_activity_notifications LIKE 'subject_type'" )->fetch ();
		if (! empty ( $type_array )) {
			$varchar = $type_array ['Type'];
			$length_varchar = explode ( "(", $varchar );
			$length = explode ( ")", $length_varchar [1] );
			$length_type = $length [0];
			if ($length_type < 32) {
				$run_query = $db->query ( "ALTER TABLE `engine4_activity_notifications` CHANGE `subject_type` `subject_type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
			}
		}
		
		$pageTime = time ();
		$db->query ( "INSERT IGNORE INTO `engine4_core_settings` (`name`, `value`) VALUES
		('sitepage.basetime', $pageTime ),
		('sitepage.isvar', 0 ),
		('sitepage.filepath', 'Sitepage/controllers/license/license2.php');" );
		
		//CODE FOR INCREASE THE SIZE OF engine4_activity_notifications's FIELD type
		$type_array = $db->query ( "SHOW COLUMNS FROM engine4_activity_notifications LIKE 'object_type'" )->fetch ();
		if (! empty ( $type_array )) {
			$varchar = $type_array ['Type'];
			$length_varchar = explode ( "(", $varchar );
			$length = explode ( ")", $length_varchar [1] );
			$length_type = $length [0];
			if ($length_type < 32) {
				$run_query = $db->query ( "ALTER TABLE `engine4_activity_notifications` CHANGE `object_type` `object_type` VARCHAR( 32 ) CHARACTER SET latin1 COLLATE latin1_general_ci NOT NULL" );
			}
		}
		
		//
		// Mobile Pages Home
		// page
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'sitepage_mobi_home' )->limit ( 1 );
		;
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			$db->insert ( 'engine4_core_pages', array ('name' => 'sitepage_mobi_home', 'displayname' => 'Mobile Pages Home', 'title' => 'Mobile Pages Home', 'description' => 'This is the mobile verison of a Pages home page.', 'custom' => 0 ) );
			$page_id = $db->lastInsertId ( 'engine4_core_pages' );
			
			// containers
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'main', 'parent_content_id' => null, 'order' => 1, 'params' => '' ) );
			$container_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'middle', 'parent_content_id' => $container_id, 'order' => 2, 'params' => '' ) );
			$middle_id = $db->lastInsertId ( 'engine4_core_content' );
			
			// widgets entry
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.browsenevigation-sitepage', 'parent_content_id' => $middle_id, 'order' => 1, 'params' => '{"title":"","titleCount":"true"}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.zeropage-sitepage', 'parent_content_id' => $middle_id, 'order' => 3, 'params' => '{"title":"","titleCount":"true"}' ) );
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.search-sitepage', 'parent_content_id' => $middle_id, 'order' => 2, 'params' => '{"title":"","titleCount":"true"}' ) );
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.recently-popular-random-sitepage', 'parent_content_id' => $middle_id, 'order' => 4, 'params' => '{"title":"","titleCount":"true"}' ) );
		}
		
		// Mobile Browse Pages
		// page
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'sitepage_mobi_index' )->limit ( 1 );
		;
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			$db->insert ( 'engine4_core_pages', array ('name' => 'sitepage_mobi_index', 'displayname' => 'Mobile Browse Pages', 'title' => 'Mobile Browse Pages', 'description' => 'This is the mobile verison of a pages browse page.', 'custom' => 0 ) );
			$page_id = $db->lastInsertId ( 'engine4_core_pages' );
			
			// containers
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'main', 'parent_content_id' => null, 'order' => 1, 'params' => '' ) );
			$container_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'middle', 'parent_content_id' => $container_id, 'order' => 2, 'params' => '' ) );
			$middle_id = $db->lastInsertId ( 'engine4_core_content' );
			
			// widgets entry
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.browsenevigation-sitepage', 'parent_content_id' => $middle_id, 'order' => 1, 'params' => '{"title":"","titleCount":"true"}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.search-sitepage', 'parent_content_id' => $middle_id, 'order' => 2, 'params' => '{"title":"","titleCount":"true"}' ) );
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.pages-sitepage', 'parent_content_id' => $middle_id, 'order' => 3, 'params' => '{"title":"","titleCount":"true"}' ) );
		}
		
		//
		// Mobile Pages Profile
		// page
		// Check if it's already been placed
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_pages' )->where ( 'name = ?', 'sitepage_mobi_view' )->limit ( 1 );
		;
		$info = $select->query ()->fetch ();
		
		if (empty ( $info )) {
			$db->insert ( 'engine4_core_pages', array ('name' => 'sitepage_mobi_view', 'displayname' => 'Mobile Page Profile', 'title' => 'Mobile Page Profile', 'description' => 'This is the mobile verison of a listing profile.', 'custom' => 0 ) );
			$page_id = $db->lastInsertId ( 'engine4_core_pages' );
			
			// containers
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'main', 'parent_content_id' => null, 'order' => 1, 'params' => '' ) );
			$container_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'container', 'name' => 'middle', 'parent_content_id' => $container_id, 'order' => 2, 'params' => '' ) );
			$middle_id = $db->lastInsertId ( 'engine4_core_content' );
			
			// widgets entry
			

			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.title-sitepage', 'parent_content_id' => $middle_id, 'order' => 1, 'params' => '{"title":"","titleCount":"true"}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.mainphoto-sitepage', 'parent_content_id' => $middle_id, 'order' => 2, 'params' => '{"title":"","titleCount":"true"}' ) );
			
			// middle tabs
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'core.container-tabs', 'parent_content_id' => $middle_id, 'order' => 4, 'params' => '{"max":"6"}' ) );
			$tab_middle_id = $db->lastInsertId ( 'engine4_core_content' );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'socialengineaddon.feed', 'parent_content_id' => $tab_middle_id, 'order' => 1, 'params' => '{"title":"What\'s New","titleCount":"true"}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.info-sitepage', 'parent_content_id' => $tab_middle_id, 'order' => 2, 'params' => '{"title":"Info","titleCount":"true"}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.overview-sitepage', 'parent_content_id' => $tab_middle_id, 'order' => 3, 'params' => '{"title":"Overview","titleCount":"true"}' ) );
			
			$db->insert ( 'engine4_core_content', array ('page_id' => $page_id, 'type' => 'widget', 'name' => 'sitepage.location-sitepage', 'parent_content_id' => $tab_middle_id, 'order' => 4, 'params' => '{"title":"Map","titleCount":"true"}' ) );
		}
		
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepage' )->where ( 'version >= ?', '4.1.6' )->where ( 'version < ?', '4.1.7' );
		$oldVersion = $select->query ()->fetchObject ();
		if (! empty ( $oldVersion )) {
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_settings' )->where ( 'name = ?', 'sitepage.profile.search' )->limit ( 1 );
			$info = $select->query ()->fetch ();
			if (! empty ( $info )) {
				if ($info ['value'] == 1) {
					$db->update ( 'engine4_sitepage_searchform', array ('display' => $info ['value'] ), array ('name = ?' => 'profile_type' ) );
				} else {
					$db->update ( 'engine4_sitepage_searchform', array ('display' => $info ['value'] ), array ('name = ?' => 'profile_type' ) );
				}
				$db->delete ( 'engine4_core_settings', array ('name = ?' => 'sitepage.profile.search' ) );
			}
		}
		
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'sitepage' )->where ( 'version < ?', '4.1.7p1' );
		$is_latestversion = $select->query ()->fetchObject ();
		if (! empty ( $is_latestversion )) {
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_activity_actiontypes' )->where ( 'type =?', 'sitepage_profile_photo_update' )->where ( 'module =?', 'sitepage' )->limit ( 1 );
			$fetchInfo = $select->query ()->fetch ();
			if (empty ( $fetchInfo )) {
				$db->insert ( 'engine4_activity_actiontypes', array ('type' => 'sitepage_profile_photo_update', 'module' => 'sitepage', 'body' => '{item:$subject} changed their Page profile photo.', 'enabled' => 1, 'displayable' => 3, 'attachable' => 2, 'commentable' => 1, 'shareable' => 1, 'is_generated' => 1 )

				 );
			}
			
			$select = new Zend_Db_Select ( $db );
			$select->from ( 'engine4_core_pages' )->where ( 'name =?', 'sitepage_index_index' )->limit ( 1 );
			$fetchPageId = $select->query ()->fetch ();
			if (! empty ( $fetchPageId )) {
				$select = new Zend_Db_Select ( $db );
				$select = $select->from ( 'engine4_core_content' )->where ( 'page_id =?', $fetchPageId ['page_id'] )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'top' )->limit ( 1 );
				$container_id = $select->query ()->fetch ();
				if (! empty ( $container_id )) {
					$select = new Zend_Db_Select ( $db );
					$select = $select->from ( 'engine4_core_content' )->where ( 'page_id =?', $fetchPageId ['page_id'] )->where ( 'type = ?', 'container' )->where ( 'name = ?', 'middle' )->where ( 'parent_content_id = ?', $container_id ['content_id'] )->limit ( 1 );
					$middle_id = $select->query ()->fetch ();
					if (! empty ( $middle_id )) {
						$select = new Zend_Db_Select ( $db );
						$select = $select->from ( 'engine4_core_content' )->where ( 'page_id =?', $fetchPageId ['page_id'] )->where ( 'name = ?', 'sitepage.alphabeticsearch-sitepage' )->where ( 'parent_content_id = ?', $middle_id ['content_id'] )->limit ( 1 );
						$fetchWidgetContentId = $select->query ()->fetchAll ();
						if (empty ( $fetchWidgetContentId )) {
							$db->insert ( 'engine4_core_content', array ('page_id' => $fetchPageId ['page_id'], 'type' => 'widget', 'name' => 'sitepage.alphabeticsearch-sitepage', 'parent_content_id' => $middle_id ['content_id'], 'order' => 4, 'params' => '{"title":"","titleCount":"true"}' ) );
						}
					}
				}
			}
			
			$adColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_packages LIKE 'ads'" )->fetch ();
			if (empty ( $adColumn )) {
				$run_query = $db->query ( "ALTER TABLE `engine4_sitepage_packages` ADD `ads` BOOL NOT NULL DEFAULT '1'" );
				$select = new Zend_Db_Select ( $db );
				$select->from ( 'engine4_core_modules' )->where ( 'name = ?', 'communityad' );
				$is_enabled = $select->query ()->fetchObject ();
				if ($is_enabled) {
					$select = new Zend_Db_Select ( $db );
					$select->from ( 'engine4_core_settings' )->where ( 'name = ?', 'sitepage.communityads' )->limit ( 1 );
					$info = $select->query ()->fetch ();
					if (! empty ( $info )) {
						$communitadSetting = $info ['value'];
						if (! empty ( $communitadSetting )) {
							$select = new Zend_Db_Select ( $db );
							$select->from ( 'engine4_core_settings' )->where ( 'name = ?', 'sitepage.adwithpackage' )->limit ( 1 );
							$info = $select->query ()->fetch ();
							if (! empty ( $info )) {
								$showAdWithPackage = $info ['value'];
								if (! empty ( $showAdWithPackage )) {
									$select = new Zend_Db_Select ( $db );
									$select->from ( 'engine4_sitepage_packages' )->where ( 'price > ?', 0 );
									$info = $select->query ()->fetchAll ();
									foreach ( $info as $data ) {
										$db->update ( 'engine4_sitepage_packages', array ('ads' => $showAdWithPackage ), array ('package_id = ?' => $data ['package_id'] ) );
									}
								} else {
									$select = new Zend_Db_Select ( $db );
									$select->from ( 'engine4_sitepage_packages' )->where ( 'price > ?', 0 );
									$info = $select->query ()->fetchAll ();
									foreach ( $info as $data ) {
										$db->update ( 'engine4_sitepage_packages', array ('ads' => $showAdWithPackage ), array ('package_id = ?' => $data ['package_id'] ) );
									}
									$select = new Zend_Db_Select ( $db );
									$select->from ( 'engine4_sitepage_packages' )->where ( 'price = ?', 0 );
									$info = $select->query ()->fetchAll ();
									foreach ( $info as $data ) {
										$db->update ( 'engine4_sitepage_packages', array ('ads' => 1 ), array ('package_id = ?' => $data ['package_id'] ) );
									}
								}
								$db->delete ( 'engine4_core_settings', array ('name = ?' => 'sitepage.adwithpackage' ) );
							}
						}
					}
				}
			}
		}
		
		//DROP THE INDEX FROM THE "engine4_sitepage_itemofthedays" TABLE
		$itemofthedayResults = $db->query ( "SHOW INDEX FROM `engine4_sitepage_itemofthedays` WHERE Key_name = 'itemoftheday_id'" )->fetch ();
		if (! empty ( $itemofthedayResults )) {
			$db->query ( "ALTER TABLE engine4_sitepage_itemofthedays DROP INDEX itemoftheday_id" );
		}
		
		$table_exist = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_albums'" )->fetch ();
		if (! empty ( $table_exist )) {
			//ADD THE INDEX FROM THE "engine4_sitepageevent_membership" TABLE
			$ownerIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_albums` WHERE Key_name = 'owner_id'" )->fetch ();
			
			if (empty ( $ownerIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_albums` ADD INDEX ( `owner_id` );" );
			}
			
			//DROP THE COLUMN FROM THE "engine4_sitepage_albums" TABLE
			$ownerTypeColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_albums LIKE 'owner_type'" )->fetch ();
			if (! empty ( $ownerTypeColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_albums` DROP `owner_type`" );
			}
			
			//DROP THE COLUMN FROM THE "engine4_sitepage_albums" TABLE
			$typeTypeColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_albums LIKE 'type'" )->fetch ();
			if (! empty ( $typeTypeColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_albums` CHANGE `type` `type` ENUM( 'note', 'overview', 'wall' ) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL;" );
			}
		
		}
		
		$table_exist = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_claims'" )->fetch ();
		if (! empty ( $table_exist )) {
			//ADD THE INDEX FROM THE "engine4_sitepage_claims" TABLE
			$pageIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_claims` WHERE Key_name = 'page_id'" )->fetch ();
			
			if (empty ( $pageIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_claims` ADD INDEX ( `page_id` );" );
			}
			
			//ADD THE INDEX FROM THE "engine4_sitepage_claims" TABLE
			$userIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_claims` WHERE Key_name = 'user_id'" )->fetch ();
			
			if (empty ( $userIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_claims` ADD INDEX ( `user_id` );" );
			}
		}
		
		$table_exist = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_contentpages`'" )->fetch ();
		if (! empty ( $table_exist )) {
			//ADD THE INDEX FROM THE "engine4_sitepage_contentpages" TABLE
			$pageIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_contentpages`` WHERE Key_name = 'page_id'" )->fetch ();
			
			if (empty ( $pageIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_contentpages`` ADD INDEX ( `page_id` );" );
			}
			
			//ADD THE INDEX FROM THE "engine4_sitepage_contentpages" TABLE
			$userIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_contentpages`` WHERE Key_name = 'user_id'" )->fetch ();
			
			if (empty ( $userIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_contentpages`` ADD INDEX ( `user_id` );" );
			}
			
			//ADD THE INDEX FROM THE "engine4_sitepage_contentpages" TABLE
			$nameColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_contentpages`` WHERE Key_name = 'name'" )->fetch ();
			
			if (empty ( $nameColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_contentpages`` ADD INDEX ( `name` );" );
			}
		
		}
		
		$table_exist = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_favourites'" )->fetch ();
		if (! empty ( $table_exist )) {
			//ADD THE INDEX FROM THE "engine4_sitepage_favourites" TABLE
			$pageIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_favourites` WHERE Key_name = 'page_id'" )->fetch ();
			
			if (empty ( $pageIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_favourites` ADD INDEX ( `page_id` );" );
			}
			
			//ADD THE INDEX FROM THE "engine4_sitepage_favourites" TABLE
			$ownerIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_favourites` WHERE Key_name = 'owner_id'" )->fetch ();
			
			if (empty ( $ownerIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_favourites` ADD INDEX ( `owner_id` );" );
			}
		}
		
		$table_exist = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_manageadmins'" )->fetch ();
		if (! empty ( $table_exist )) {
			//ADD THE INDEX FROM THE "engine4_sitepage_manageadmins" TABLE
			$pageIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_manageadmins` WHERE Key_name = 'page_id'" )->fetch ();
			
			if (empty ( $pageIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_manageadmins` ADD INDEX ( `page_id` );" );
			}
		}
		
		$table_exist = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_pagestatistics'" )->fetch ();
		if (! empty ( $table_exist )) {
			//ADD THE INDEX FROM THE "engine4_sitepage_pagestatistics" TABLE
			$pageIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_pagestatistics` WHERE Key_name = 'page_id'" )->fetch ();
			
			if (empty ( $pageIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_pagestatistics` ADD INDEX ( `page_id` );" );
			}
			
			//ADD THE INDEX FROM THE "engine4_sitepage_pagestatistics" TABLE
			$viewerIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_pagestatistics` WHERE Key_name = 'viewer_id'" )->fetch ();
			
			if (empty ( $viewerIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_pagestatistics` ADD INDEX ( `viewer_id` );" );
			}
		}
		
		$table_exist = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_listmemberclaims'" )->fetch ();
		if (! empty ( $table_exist )) {
			//ADD THE INDEX FROM THE "engine4_sitepage_listmemberclaims" TABLE
			$userIdColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_listmemberclaims` WHERE Key_name = 'user_id'" )->fetch ();
			
			if (empty ( $userIdColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_listmemberclaims` ADD INDEX ( `user_id` );" );
			}
		}
		
		//QUERIES TRANFER FROM UPGRADE FILE OF 4.1.7P2
		$itemTable = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_itemofthedays'" )->fetch ();
		if (! empty ( $itemTable )) {
			
			$titleColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_itemofthedays LIKE 'title'" )->fetch ();
			if (! empty ( $titleColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` DROP `title`" );
			}
			
			$pageIdColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_itemofthedays LIKE 'page_id'" )->fetch ();
			$endTimeColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_itemofthedays LIKE 'endtime'" )->fetch ();
			$dateColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_itemofthedays LIKE 'date'" )->fetch ();
			
			$endDateColoum = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_itemofthedays LIKE 'end_date'" )->fetch ();
			$startDateColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_itemofthedays LIKE 'start_date'" )->fetch ();
			$dateColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_itemofthedays` WHERE Key_name = 'date'" )->fetch ();
			$endTimeColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_itemofthedays` WHERE Key_name = 'endtime'" )->fetch ();
			$endDateColoumIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_itemofthedays` WHERE Key_name = 'end_date'" )->fetch ();
			$startDateColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_itemofthedays` WHERE Key_name = 'start_date'" )->fetch ();
			
			if (! empty ( $dateColumn ) && empty ( $dateColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` ADD INDEX ( `date` );" );
			}
			
			if (! empty ( $endTimeColumn ) && empty ( $endTimeColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` ADD INDEX ( `endtime` );" );
			}
			
			if (! empty ( $endDateColoum ) && empty ( $endDateColoumIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` ADD INDEX ( `end_date` );" );
			}
			
			if (! empty ( $startDateColumn ) && empty ( $startDateColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` ADD INDEX ( `start_date` );" );
			}
			
			if (! empty ( $pageIdColumn ) && ! empty ( $endTimeColumn ) && ! empty ( $dateColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` CHANGE `page_id` `resource_id` INT( 11 ) NOT NULL ,
					CHANGE `endtime` `end_date` DATE NOT NULL, CHANGE `date` `start_date` DATE NOT NULL" );
			}
			
			$resourceTypeColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_itemofthedays LIKE 'resource_type'" )->fetch ();
			
			$resourceTypeColumnIndex = $db->query ( "SHOW INDEX FROM `engine4_sitepage_itemofthedays` WHERE Key_name = 'resource_type'" )->fetch ();
			
			if (empty ( $resourceTypeColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` ADD `resource_type` VARCHAR( 64 ) NOT NULL" );
			}
			
			if (! empty ( $resourceTypeColumn ) && empty ( $resourceTypeColumnIndex )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_itemofthedays` ADD INDEX (`resource_type`)" );
				$db->query ( "UPDATE `engine4_sitepage_itemofthedays` SET `resource_type` = 'sitepage_page' WHERE `engine4_sitepage_itemofthedays` .`resource_type` = ''" );
			}
		}
		
		$pageTable = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_pages'" )->fetch ();
		$networkPrivacyColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_pages LIKE 'networks_privacy'" )->fetch ();
		if (! empty ( $pageTable )) {
			if (empty ( $networkPrivacyColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_pages` ADD `networks_privacy` MEDIUMTEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL DEFAULT NULL" );
			}
			
			$subsubCategoryIdColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_pages LIKE 'subsubcategory_id'" )->fetch ();
			if (empty ( $subsubCategoryIdColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_pages` ADD `subsubcategory_id` INT( 11 ) NOT NULL" );
			}
		
		}
		
		$categoryTable = $db->query ( "SHOW TABLES LIKE 'engine4_sitepage_categories'" )->fetch ();
		if (! empty ( $categoryTable )) {
			
			$userIdColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_categories LIKE 'user_id'" )->fetch ();
			if (! empty ( $userIdColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_categories` DROP `user_id`" );
			}
			
			$subcatDependencyColumn = $db->query ( "SHOW COLUMNS FROM engine4_sitepage_categories LIKE 'subcat_dependency'" )->fetch ();
			if (empty ( $subcatDependencyColumn )) {
				$db->query ( "ALTER TABLE `engine4_sitepage_categories` ADD `subcat_dependency` INT( 11 ) NOT NULL" );
			}
		
		}
		$db = $this->getDb ();
		$db->query ( 'INSERT IGNORE INTO `engine4_activity_actiontypes` (`type`, `module`, `body`, `enabled`, `displayable`, `attachable`, `commentable`, `shareable`, `is_generated`) VALUES
(\'sitepage_post\', \'sitepage\', \'{actors:$subject:$object}: {body:$body}\', 1, 3, 1, 1, 1, 0),
(\'sitepage_post_self\', \'sitepage\', \'{item:$subject} {body:$body}\', 1, 1, 1, 1, 1, 0);' );
		parent::onInstall ();
	}
	
	function onDisable() {
		$db = $this->getDb ();
		$select = new Zend_Db_Select ( $db );
		$select->from ( 'engine4_core_modules', array ('name' ) )->where ( 'enabled = ?', 1 );
		$moduleData = $select->query ()->fetchAll ();
		
		$subModuleArray = array ("sitepagealbum", "sitepagebadge", "sitepagediscussion", "sitepagedocument", "sitepageevent", "sitepageform", "sitepageinvite", "sitepagenote", "sitepageoffer", "sitepagepoll", "sitepagereview", "sitepagevideo", "sitepagemusic", "sitepagewishlist" );
		
		foreach ( $moduleData as $key => $moduleName ) {
			if (in_array ( $moduleName ['name'], $subModuleArray )) {
				$base_url = Zend_Controller_Front::getInstance ()->getBaseUrl ();
				$error_msg1 = Zend_Registry::get ( 'Zend_Translate' )->_ ( 'Note: Please disable all the integrated sub-modules of Pages Plugin before disabling the Pages Plugin itself.' );
				echo "<div style='background-color: #E9F4FA;border-radius:7px 7px 7px 7px;float:left;overflow: hidden;padding:10px;'><div style='background:#FFFFFF;border:1px solid #D7E8F1;overflow:hidden;padding:20px;'><span style='color:red'>$error_msg1</span><br/> <a href='" . $base_url . "/manage'>Click here</a> to go Manage Packages.</div></div>";
				die ();
			}
		}
		
		parent::onDisable ();
	}

}

?>